<?php
namespace Admin\Controller;
use Think\Controller;
class MainController extends BaseController {
	protected $goods_model;
    protected $buy_model;
    protected $user_model;
    
    public function _initialize(){
        layout(false);
        parent::_initialize();
        $this->goods_model = M('Goods');
        $this->buy_model = M('Buy');
        $this->user_model = M('User');
    }
    //首页规则展示
    public function index(){
    	$this->assign('role',session('role'));
        $user_id=session('user_id')?session('user_id'):0;
    	$uOb = M('User');
    	$result = $uOb->where('user_id='.$user_id)->field('refer')->find();
    	if($result) {
    	    $this->assign('user_id',$result['refer']);
        }else{
    	    $this->assign('user_id','无');
        }
        $this->display();
    }
    public function menu() {
        $this->display();
    }

    public function aUser(){
    	$this->display();
    }
    //用户分页接口
    public function aUserPages(){
    	$uOb=M('User');
    	if($this->rules['agent_review'] == 1) {
            $result = $uOb->field('*')->where('status=0 and role < 10 and refer=0')->order('create_time desc')->select();
        }else{
            $result = $uOb->field('*')->where('status=0 and role < 10')->order('create_time desc')->select();
        }
        if($result) {
    	    $this->ajaxReturn($result);
        }else{
    	    $this->ajaxReturn('error');
        }
    }
    
    //审核用户
    public function activeUser(){
    	$user_id=$_GET['user_id'];
    	$uOb=M('User');
    	$re = $uOb->where('user_id='.$user_id)->find();
    	$url = C('API_URL').'/?act=setGM&uid='.$user_id.'&role='.$re['role'];
    	$curl_res = get_curl($url);
    	if($curl_res && $curl_res['code'] != 12 ) {
            $result=$uOb->where('user_id='.$user_id)->save(array('status'=>1));
            if($result) {
                $this->ajaxReturn('success');
            }else{
                $this->ajaxReturn('error');
            }
        }else{
    	    $this->ajaxReturn('error');
        }
    }
    //删除用户
    public function delUser(){
    	$user_id=$_GET['user_id'];
    	$uOb=M('User');
    	$result=$uOb->where('user_id='.$user_id)->delete();
    	if($result) {
    	    $this->ajaxReturn('success');
        }else{
    	    $this->ajaxReturn('error');
        }
    }
    //代理列表
    public function agentUserList(){
    	$this->display();
    }
    
    //代理分页情况
    public function agentUserListPage(){
    	$uOb=M('User');
    	$result = $uOb->where('status=1 and role<10')->field('*,(total_rebate-total_withdraw) wait_withdraw')->order('create_time desc')->select();
    	if($result) {
    	    $this->ajaxReturn($result);
        }else{
    	    $this->ajaxReturn('error');
        }
    }
    public function agentUserTitles(){
    	$user_id = $_GET['user_id'];
    	$state = I('state');
    	$uOb=M("User");
    	$result=$uOb->where('user_id='.$user_id)->save(array('state'=>$state));
    	if($result) {
    		$this->ajaxReturn('success');
    	}else{
    		$this->ajaxReturn('error');
    	}
    }
    //代理查询
    public function agentUserSearch(){
    	$this->display();
    }

    public function agentUserSearchh() {
        $user_id=I('user_id');
        $uOb = M('User');
        $aOb = M('Agent');
        $result = $uOb->where("user_id=".$user_id)->find();

        $data = array();
        if($result) {
            $agent = $aOb->where('level='.$result['role'])->field('agent_name')->find();
            $count = M('User')->where('refer='.$user_id)->count();

            $url = C('API_URL').'/?act=queryUser&uid='.$user_id;
            $curl_res = get_curl($url);
            if($curl_res['code'] != 12 && $curl_res != null) {
                $data['agent'] = $result;
                $data['msg'] = $curl_res;
                $data['count'] = $count;
                $data['agent_name'] = $agent['agent_name'];
                $this->ajaxReturn($data);
            }else{
                $this->ajaxReturn('error');
            }
        }else{
            $this->ajaxReturn('nodata');
        }
    }

    public function agentUserBuyDetail() {
        $user_id = I('user_id');
        $bOb = M('Buy');
        $result = $bOb->where('user_id='.$user_id.' and delete_tag=0')->order('add_time desc')->select();
        if($result) {
            $this->ajaxReturn($result);
        }else{
            $this->ajaxReturn('error');
        }
    }

    public function agentUserPayDetail(){
        $user_id = I('user_id');
        $pOb = M('Pay');
        $result = $pOb->where('user_id='.$user_id)->order('add_time desc')->select();
        if($result) {
            $this->ajaxReturn($result);
        }else{
            $this->ajaxReturn('error');
        }
    }

    //修改信息
    public function userInfoSet() {
    	$user_id=I('user_id');
    	$password=I('password');
    	$role=I('role');
    	$state=I('state');
    	$uOb=M('User');
    	$url=C('API_URL').'/?act=setGM&uid='.$user_id.'&role='.$role;
    	$curl_res=get_curl($url);
    	if($curl_res['level'] == 0) {
    	    $result = $uOb->where('user_id='.$user_id)->delete();
    	    if($result) {
    	        $this->ajaxReturn('sucdel');
            }else{
    	        $this->ajaxReturn('errdel');
            }
        }
    	if($curl_res['level'] == $role) {
            if($password == '' || $password == null) {
                $result=$uOb->where('user_id='.$user_id)->save(array('role'=>$role,'state'=>$state));
                if($result) {
                    $this->ajaxReturn('success');
                }else{
                    $this->ajaxReturn('error');
                }
            }else if($password != '' || $password != null) {
                $result=$uOb->where('user_id='.$user_id)->save(array('password'=>md5($password),'state'=>$state,'role'=>$role));
                if($result) {
                    $this->ajaxReturn('success');
                }else{
                    $this->ajaxReturn('error');
                }
            }
        }else if($curl_res['code'] == 12) {
    	    $this->ajaxReturn('faliure');
        }

    }
    
    
    
    //购买记录
    public function agentUserBuy(){
        $this->display();
    }
    
    //购买记录页面
    public function agentUserBuyPage(){
    	$bOb=M('Buy');
    	$page=I('page');
    	$num=10;
    	$count=$bOb->order('add_time desc')->where('status=1')->count();
        $tiao=($page-1)*$num;
        $re = $bOb->order('add_time desc')->where('status=1')->limit($tiao,$num)->select();
        $pageSize=ceil($count/$num);
        $data=array();
        $data['page']=$page;
        $data['pageSize']=$pageSize;
        $data['data']=$re;
        if($re) {
        	$this->ajaxReturn($data);
        }else{
        	$this->ajaxReturn('error');
        }
    }
    
    
    //显示数据的条数
     public function agentUserPage(){
    	$pOb=M('Pay');
    	$user_id=I('user_id');
    	$num=2;
    	$result=$pOb->where('user_id='.$user_id)->count();
    	$pnum=ceil($result/$num);
    	if($pnum){
    		$this->ajaxReturn($pnum);
    	}else{
    		$this->ajaxReturn(0);
    	}
    }
    //用户查询的所有数据
   	public function aUserPage(){
   		$uOb=M('User');
   		$page=I('page');
   		$num=10;
   		$tiao=($page-1)*$num;
   		$result=$uOb->where('status=0')->limit($tiao,$num)->select();
   		
   		if($result) {
   			$this->ajaxReturn($result);
   		}else{
   			$this->ajaxReturn('false');
   		}
   	}
   	//充值排行
   	function payRank(){
   		$this->display();
   	}
   	
   	//充值排行分页
   	function payRankPage(){
   		$pOb=M('Pay');
   		$now_time = time();
   		$time = $now_time - 604800;
   		
		$result=$pOb->alias('p')
   			  ->join('__USER__ u on u.user_id=p.user_id')
   			  ->where('add_time >= '.$time.' and add_time <= '.$now_time)
   			  ->group('user_id')
   			  ->field('p.*,u.name,SUM(number) as total_number')
   			  ->order('total_number desc')
   			  ->select();
  		
   		if($result) {
   			$this->ajaxReturn($result);
   		}else{
   			$this->ajaxReturn('nodata');
   		}
   	}
   	
   	//支付时间查询
   	public function payRankSearch(){
   		$pOb=M('Pay');
   		$start_time = I('start_time');
   		$end_time = I('end_time');
  		
   		$result=$pOb->alias('p')
   		->join('__USER__ u on u.user_id=p.user_id')
   		->where('add_time >= '.$start_time.' and add_time <= '.$end_time)
   		->group('user_id')
   		->field('p.*,u.name,SUM(number) as total_number')
   		->order('total_number desc')
	    ->select();
   	
   		if($result) {
   			$this->ajaxReturn($result);
   		}else{
   			$this->ajaxReturn('nodata');
   		}
   	}
   	
   	//购买排行
   	public function buyRank(){
   		$this->display();
   	}
   	//购买排行分页
   	function buyBankPage(){
   		$bOb=M('Buy');
   		$now_time = time();
   		$time = $now_time - 604800;
		$result = $bOb->alias('b')
   			  ->join('__USER__ u on u.user_id=b.user_id')
   			  ->where('review_time >= '.$time.' and review_time <= '.$now_time)
   			  ->group('user_id')
   			  ->field('b.*,u.name,SUM(amount) as total_number')
   			  ->order('total_number desc')
   			  ->select();
   		if($result) {
   			$this->ajaxReturn($result);
   		}else{
   			$this->ajaxReturn('nodata');
   		}
   	}
   	
   	//购买时间查询
   	public function buyRankSearch(){
   		$bOb=M('Buy');
   		$start_time = I('start_time');
   		$end_time = I('end_time');
   		$result=$bOb->alias('b')
   			  ->join('__USER__ u on u.user_id=b.user_id')
   			  ->where('review_time >= '.$start_time.' and review_time <= '.$end_time)
   			  ->group('user_id')
   			  ->field('b.*,u.name,SUM(amount) as total_number')
   			  ->order('total_number desc')
   			  ->select();
   
   		if($result) {
   			$this->ajaxReturn($result);
   		}else{
   			$this->ajaxReturn('nodata');
   		}
   	}
   	
   	//返回数据的分页条数
   	public function maxPage(){
   		$uOb=M('User');
   		$num=10;
   		$result=$uOb->where('status=0')->count();
   		$pnum=ceil($result/$num);
   		if($pnum) {
   			$this->ajaxReturn($pnum);
   		}else{
   			$this->ajaxReturn(0);
   		}
   	}
   	
   	//商品列表
   	public function goodsList(){
   		$this->display();
   	}
   	
   	//商品列表页面
   	public function goodsListPage() {
   		$gOb=M('Goods');
   		$result = $gOb->order('add_time desc')->select();
   		if($result) {
   		    $this->ajaxReturn($result);
        }else{
   		    $this->ajaxReturn('error');
        }
   	}
   	
   	//商品删除
   	public function goodsDelete(){
   		$goods_id=I('goods_id');
   		$gOb=M('Goods');
   		$result=$gOb->where('goods_id='.$goods_id)->delete();
   		if($result) {
   			$this->ajaxReturn('success');
   		}else{
   			$this->ajaxReturn('failure');
   		}
   	}
   	
   	//商品修改页面
   	public function goodsModify(){
   		$goods_id=I('goods_id');
   		$this->assign('goods_id',$goods_id);
   		$this->display();
   	}
    public function goodsModifyData() {
   	    $goods_id = I('goods_id');
   	    $gOb = M('Goods');
   	    $result = $gOb->where('goods_id='.$goods_id)->find();
   	    if($result) {
   	        $this->ajaxReturn($result);
        }else{
   	        $this->ajaxReturn('nodata');
        }
    }
   	//商品修改保存页面
   	public function goodsSave(){
   		$goods_id=$_GET['goods_id']?$_GET['goods_id']:'';
   		$gOb=M('Goods');
   		$data=I('post.');
   		if($goods_id) {
   			$data['modify_time'] = time();
   			$result=$gOb->where('goods_id='.$goods_id)->save($data);
   		}else{
   			$data['add_time'] = time();
   			$result=$gOb->add($data);
   		}
   		
   		if($result) {
   			$this->ajaxReturn('success');
   		}else{
   			$this->ajaxReturn('failure');
   		}
   	}
   	
   	//商品搜索
   	public function goodsSearch(){
   		$this->display();
   	}
   	
   	//搜索的接口
   	public function goodsSearchCheck(){
   		$goods_id=I('goods_id');
   		$gOb=M('Goods');
   		$result=$gOb->where('goods_id='.$goods_id)->find();
   		if($result) {
   			$this->ajaxReturn($result);
   		}else{
   			$this->ajaxReturn('false');
   		}
   	}
   	
   	//商品销售
   	public function goodsSell(){
   		$this->display();
   	}  
   	//商品销售分页
   	public function goodsSellPage(){
   		$page=I('page');
   		$num=10;
   		$bOb=M('Buy');
   		$tiao=($page-1)*$num;
   		$count=$bOb->where('delete_tag=0')->count();
   		$pageSize=ceil($count/$num);
   		$result=$bOb->where('delete_tag=0')->order('add_time desc')->limit($tiao,$num)->select();
   		$data=array();
   		$data['data']=$page;
   		$data['pageSize']=$pageSize;
   		$data['data']=$result;
   		if($result) {
   			$this->ajaxReturn($data);
   		}else{
   			$this->ajaxReturn('error');
   		}
   	}
   	 	
   	//购买房卡
   	public function buy(){
   		$url=C('API_URL').'/?act=getResource&uid='.session('user_id');
   		$curl_res=get_curl($url);
   		
   		$user_id=session('user_id') ? session('user_id') : 0;
   		$gOb=M('Goods');
   		$uOb=M('User');
   		$goods = $gOb->where('status=1')->order('order_num')->select();
   		$user=$uOb->field('total_amount,role')->where('user_id='.$user_id)->find();
// 		$shortfall = $this->rules['agent_active_amount'] - $user['total_amount'];
   		
// 		if($shortfall > 0) {
// 			$this->assign('shortfall',$shortfall);
// 		}
   		
   		$this->assign('card',$curl_res['card']);
   		$this->assign('goods',$goods);
   		$this->display();
   	}
   	
   	//房卡购买确认页面
   	public function buyOrder(){
   		$user_id=session('user_id')?session('user_id'):0;
   		$uOb=M('User');
   		$gOb=M('Goods');
   		$user=$uOb->field('status')->where(array('user_id'=>$user_id))->find();
   		if($user['status'] == 0) {
   			$this->assign('allow_buy','no');
   		}else{
   			$goods_id = $_GET['goods_id'];
   			$result=$gOb->where(array('goods_id'=>$goods_id))->find();
   			$this->assign('result',$result);
   		}
   		$this->display();
   	}
   	
   	//购买保存
   	public function buySave(){
   		$user_id=session('user_id')?session('user_id'):0;
        $goods_id=$_GET['goods_id'];

   		$gOb=M('Goods');
   		$bOb=M('Buy');
   		$goods=$gOb->where('goods_id='.$goods_id)->find();
   		
   		if($goods) {
   			$data=I('post.');
            $data['goods_name']=$goods['goods_name'];
            $data['goods_id']=$goods['goods_id'];
            $data['number']=$goods['number'];
            $data['amount']=$goods['price'];
            $data['user_id']=$user_id;
            $url=C('API_URL').'/?act=queryUser&uid='.$user_id;
            $curl_res=get_curl($url);
            if($curl_res['code'] != 12 && $curl_res['_id']) {
                $res = $curl_res['info']['name'];
            }else{
                $res = '';
            }
            $data['name'] = $res;
            $data['add_time']=time();
            $data['status']=0;
           	$bOb->add($data);
            $this->ajaxReturn('success');
   		}else{
   			$this->ajaxReturn('error');
   		}
   	}
   	
   	//代理购买记录
   	public function buyReview(){
        $this->display();
   	}
   	
   	//代理购买分页
   	public function buyReviewPage(){
   		$result=$this->buy_model->alias('b')
            ->join('__USER__ u on u.user_id=b.user_id')
            ->field('u.tel, u.name, b.*')
            ->order('add_time desc')
            ->where('b.status=0 and b.delete_tag=0')
            ->select();
        if($result) {
        	$this->ajaxReturn($result);
        }else{
        	$this->ajaxReturn('error');
        }
   	}
   	
   		
   	//删除审核
   	public function delBuyReview(){
   		$id=$_GET['id'];
   		$bOb=M('Buy');
   		$result=$bOb->where('id='.$id)->save(array('status'=>1,'delete_tag'=>1,'delete_time'=>time()));
   	
        if($result){
        	$this->ajaxReturn('success');
        }else{
        	$this->ajaxReturn('error');
        }
   	}
   	
   	
   	//购买审核通过
   	public function buyReviewSave(){
   		$id=$_GET['id'];
   		$bOb=M('Buy');
   		$uOb=M('User');
// 		$result=$bOb->where('id='.$id.' and status=0')->save(array('status'=>1,'review_time'=>time()));
   		$buy=$bOb->field('user_id,amount,number')->where('id='.$id)->find();
		$user_id=$buy['user_id'];
		$buy_id=$id;
		
   		$url=C('API_URL').'/?act=addResource&uid='.$buy['user_id'].'&num='.$buy['number'];
		$re = get_curl($url);
   		if($re['code'] == 12) {
   			$this->ajaxReturn('failure');
   		}else if($re == '') {
   			$this->ajaxReturn('longtime');
   		}else if($re['card']) {
   			$result=$bOb->where('id='.$buy_id.' and status=0')->save(array('status'=>1,'review_time'=>time()));

            $uOb->where('user_id='.$user_id)->setInc('total_amount',$buy['amount']);

   			$user=$this->user_model->field('total_amount,role,refer')->where('user_id='.$user_id)->find();
   			 if ($user['refer'] && $user['role'] > 1 && $user['role'] <= $this->rules['agent_level']) {
                $agent = M('agent')->where('level='.$user['role'])->find();

                $rebate_model = M('Rebate');

                $rebate = array();
                $rebate['buy_user_id']=$user_id;
                $rebate['buy_id']=$buy_id;
                $rebate['add_time']=time();
                
                if ($agent['agent_rebate1']) {
                    $rebate['user_id']=$user['refer'];
                    $rebate['rebate_scale']=$agent['agent_rebate1'];
                    $rebate['rebate']=round($buy['amount']*($agent['agent_rebate1']/100),2);
                    $rebateResult=$rebate_model->add($rebate);
                    if ($rebateResult) {
                        $this->user_model->where('user_id='.$user['refer'])->setInc('total_rebate',$rebate['rebate']);
                    }
                }
                
                if ($agent['agent_rebate2']) {
                    $refer_user = $this->user_model->field('total_amount,role,refer')->where('user_id='.$user['refer'])->find();
                    if ($refer_user && $refer_user['refer'] && $refer_user['role'] >= 1 && $refer_user['role'] < $this->rules['agent_level']) {
                        $rebate2 = $rebate;
                        $rebate2['user_id']=$refer_user['refer'];
                        $rebate2['rebate_scale']=$agent['agent_rebate2'];
                        $rebate2['rebate']=round($buy['amount']*($agent['agent_rebate2']/100),2);
                        if ($rebate_model->add($rebate2)) {
                            $this->user_model->where('user_id='.$refer_user['refer'])->setInc('total_rebate',$rebate2['rebate']);
                        }
                    }
                }
            }
   			$this->ajaxReturn('success');
   		}else{
   			$this->ajaxReturn('error');
   		}
   	}

   	
   	//房卡购买记录
   	public function ubuyList(){
   		$this->display();
   	}

   	//房卡购买分页
   	public function ubuyListPage(){
        $bOb = M('Buy');
   		$page = I('page');
   		$num=10;
   		$tiao=($page-1)*$num;
   		$count = $bOb->where('status=1')->count();
   		$pageSize = ceil($count/$num);
   		$result = $bOb->where('status=1')->order('add_time desc')->limit($tiao,$num)->select();
   		$data=array();
   		$data['page']=$page;
   		$data['pageSize']=$pageSize;
   		$data['data']=$result;
   		if($result) {
   			$this->ajaxReturn($data);
   		}else{
   			$this->ajaxReturn('error');
   		}
   	}
   	
   	//充值页面
   	public function pay(){
   		$user_id=session('user_id')?session('user_id'):0;
   		$uOb=M('User');
   		$aOb=M('Agent');
   		
   		$url=C('API_URL').'/?act=getResource&uid='.$user_id;
   		$curl_res=get_curl($url);
   		
   		$re=$uOb->where('user_id='.$user_id)->find();
		$are=$aOb->where('level='.$re['role'])->find();
		
		$this->assign('card',$curl_res['card']);
		$this->assign('max_lines',$are['max_lines']);
		$this->display();
   	}

   	//游戏uid的详细信息
   	public function userDetailInfo(){
   		$user_id=I('user_id');
   		$url=C('API_URL').'/?act=queryUser&uid='.$user_id;
   		$curl_res=get_curl($url);
   		if($curl_res['_id']) {
   			$this->ajaxReturn($curl_res);
   		}else{
   			$this->ajaxReturn('nodata');
   		}
   	}
    //昵称查询
    public function nicknameSearch(){
   	    $this->display();
    }

    //昵称传入
    public function getNickName(){
        $name = I('name');
        $url = C('API_URL').'/?act=queryUserByName&name='.$name;
        $curl_res = get_curl($url);
        if($curl_res && $curl_res['code'] != 20) {
            $this->ajaxReturn($curl_res);
        }else{
            $this->ajaxReturn('error');
        }
    }

   	//游戏管理页面
   	public function playerManage(){
   		$this->display();
   	}
    //   	权重显示
    public function uidWeightShow(){
        $uid = I('user_id');
        $url = C('API_URL').'/?act=queryUser&uid='.$uid;
        $curl_res = get_curl($url);
        if($curl_res['code'] != 12 && $curl_res['_id']) {
            $this->ajaxReturn($curl_res);
        }else{
            $this->ajaxReturn('error');
        }
    }
    //权重设置
    public function uidConvalSave() {
        $uid = I('user_id');
        $controlValue = I('controlValue');
        $url = C('API_URL').'/?act=queryUser&uid='.$uid;
        $curl_res = get_curl($url);
        if($curl_res['controlValue'] == $controlValue) {
            $this->ajaxReturn('repeat');
        }else{
            $urll = C('API_URL').'/?act=setControlValue&uid='.$uid.'&value='.$controlValue;
            $curll_res = get_curl($urll);
            if($curll_res['value'] == $controlValue) {
                $this->ajaxReturn('success');
            }else{
                $this->ajaxReturn('error');
            }
        }
    }
    //玩家级别开通
    public function  uidRoleSet(){
        $uid=$_GET['user_id'];
        $url=C('API_URL').'/?act=queryUser&uid='.$uid;
        $curl_res=get_curl($url);
        if($curl_res['code'] ==12) {
            $this->ajaxReturn('failure');
        }else if($curl_res == '') {
            $this->ajaxReturn('longtime');
        }else{
           if((array_key_exists('gm',$curl_res)) && ($curl_res['gm']['level']>=1 && $curl_res['gm']['level'] <= $this->rules['agent_level'])) {
               $this->ajaxReturn('exist');
           }else{
               $urll=C('API_URL').'/?act=setGM&uid='.$uid.'&role='.$this->rules['agent_level'];
               $curll_res=get_curl($urll);
               if($curll_res['level'] == $this->rules['agent_level']) {
                   $uOb=M('User');
                   $data = array();
                   $data['user_id'] = $uid;
                   $password = mt_rand(100000,999999);
                   $data['password'] = md5($password);
                   $data['create_time'] = time();
                   $data['active_time'] = time();
                   $data['status'] = 1;
                   $data['state'] = 1;
                   $data['role']=$this->rules['agent_level'];
                   $uOb->add($data);
                   $reocrd = array();
                   $reocrd['password'] = $password;
                   $reocrd['state'] = 'success';
                   $this->ajaxReturn($reocrd);
               }else{
                   $this->ajaxReturn('error');
               }
           }
        }
    }
    //初始页面
    public function initSet(){
        $this->display();
    }
    //初始获取
    public function uidInit(){
        $url=C('API_URL').'/?act=getGlobalConfig&name=initResource';
        $curl_res=get_curl($url);
        if($curl_res) {
            $this->ajaxReturn($curl_res);
        }else{
            $this->ajaxReturn('error');
        }
    }
    //初始设置
    public function uidInitSet(){
        $card=I('card');
        $coin=I('coin');
        $url=C('API_URL').'/?act=setInitResource&card='.$card.'&coin='.$coin;
        $curl_res=get_curl($url);
        if($curl_res['card'] == $card && $curl_res['coin'] == $coin) {
            $this->ajaxReturn('success');
        }else if($curl_res['code']) {
            $this->ajaxReturn('failure');
        }else{
            $this->ajaxReturn('error');
        }
    }
//    幸运转盘
    public function LuckWheel(){
        $this->display();
    }

    public function getLuckWheel(){
        $url=C('API_URL').'/?act=getBigWheel';
        $curl_res=get_curl($url);
        if($curl_res['code'] != 20) {
            $this->ajaxReturn($curl_res);
        }else{
            $this->ajaxReturn('error');
        }
    }

//    设置奖励的等级
    public function setLuckWheel(){
        $data = I("params");
        $datas = json_encode($data);
//        var_dump($data);
        $url=C('API_URL').'/?act=setBigWheel&BigWheel='.$datas;
        $curl_res=get_curl($url);
        if($curl_res['code'] == 0){
            $this->ajaxReturn('success');
        }else if($curl_res['code'] == 20){
            $this->ajaxReturn('error');
        }else{
            $this->ajaxReturn('error');
        }
    }

    //开通专场
    public function uidPrivSet(){
        $uid=I('uid');
        $privilege=I('privilege');
        $url=C('API_URL').'/?act=setPrivilege&uid='.$uid.'&privilege='.$privilege;
        $curl_res=get_curl($url);
       if($curl_res && $curl_res['privilege'] == $privilege){
           $this->ajaxReturn('success');
       }else if($curl_res['code'] == 12) {
           $this->ajaxReturn('failure');
       }else{
           $this->ajaxReturn('error');
       }
    }

    //扣减数量
    public function uidResModify(){
        $rOb=M('Reduce');
        $uid=I('uid');
        $card=-I('card');
        $coin=-I('coin');
        $data = array();
        $data['uid']=$uid;
        $data['card_number']=$card;
        $data['coin_number']=$coin;
        $data['add_time']=time();
        if($card == '' || $card == 0) {
            $url=C('API_URL').'/?act=alterResource&uid='.$uid.'&type=coin&num='.$coin;
            $curl_res=get_curl($url);
            if($curl_res['code'] != 12 && $curl_res) {
                $rOb->add($data);
                $this->ajaxReturn('success');
            }else{
                $this->ajaxReturn('error');
            }
        }else if($coin == '' || $coin == 0) {
            $url=C('API_URL').'/?act=alterResource&uid='.$uid.'&type=card&num='.$card;
            $curl_res=get_curl($url);
            if($curl_res['code'] != 12 && $curl_res) {
                $rOb->add($data);
                $this->ajaxReturn('success');
            }else{
                $this->ajaxReturn('error');
            }
        }else{
            $url=C('API_URL').'/?act=alterResource&uid='.$uid.'&type=coin&num='.$coin;
            $curl_res=get_curl($url);
            $urll=C('API_URL').'/?act=alterResource&uid='.$uid.'&type=card&num='.$card;
            $curll_res=get_curl($urll);
            if(($curl_res['code'] != 12 && $curl_res) && ($curll_res['code'] != 12 && $curll_res)) {
                $rOb->add($data);
                $this->ajaxReturn('success');
            }else{
                $this->ajaxReturn('error');
            }
        }

    }
    //修改推荐人
    public function setRefer() {
        $user_id = I('user_id');
        $refer = I('refer');
        $url=C('API_URL').'/?act=changeInviter&user='.$user_id.'&inviter='.$refer;
        $curl_res = get_curl($url);
        if($curl_res && $curl_res['code'] != 20) {
            $this->ajaxReturn($curl_res);
        }else{
            $this->ajaxReturn('error');
        }
    }

   	//游戏uid的停封操作
   	public function playerHandle(){
   		$block = I('block');
   		$user_id = $_GET['user_id'];
   		$url=C('API_URL').'/?act=setBlock&uid='.$user_id.'&block='.$block;
   		$curl_res = get_curl($url);
   		
   		if($curl_res['block'] == 1 || $curl_res['block'] == 0) {
   			$this->ajaxReturn('success');
   		}else{
   			$this->ajaxReturn('error');
   		}
   	}
   	
   	
   	//充值数据保存
   	public function paySave(){
   		$user_id=session('user_id')?session('user_id'):0;
   		$num=I('number');
   		$user=I('user_id');
   		$uname=I('uname');
   		$ure=$this->user_model->where('user_id='.$user_id)->find();
   		$aOb=M('Agent');
   		$are=$aOb->where('level='.$ure['role'])->find();
   		
   		if($ure['user_id'] == $user) {
   			$this->ajaxReturn("cantpay");
   		}
   		
   		if($ure['state'] == 0) {
   			$this->ajaxReturn('invalid');
   		}
   		
   		$url=C('API_URL').'/?act=agentPay&uid='.$user_id.'&target='.I('user_id').'&num='.I('number');
   		$curl_res=get_curl($url);
   		
   		if($curl_res['code'] == 12) {
   			$this->ajaxReturn('failure');
   		}else if($curl_res == '') {
   			$this->ajaxReturn('longtime');
   		}else if($curl_res['target']['card']){
   			$pOb=M('Pay');
			$data=I('post.');
			$data['user_id']=$user_id;
			$data['add_time']=time();
			$data['status']=1;
			$data['uname']=$uname;
			$data['uid']=I('user_id');
			$re=$pOb->add($data);
			$this->user_model->where('user_id='.$user_id)->setInc('total_give',$num);
			$this->ajaxReturn('success');
   		}else{
   			$this->ajaxReturn('error');
   		}
   	}
   	
   	//充值列表
   	public function payList(){
   		$this->display();
   	}
   	
   	//充值列表分页
   	public function payListPage(){
   		$user_id=session('user_id')?session('user_id'):0;
   		$page=I('page');
   		$num=10;
   		$tiao=($page-1)*$num;

   		$pOb=M('Pay');
   		$count=$pOb->where('user_id='.$user_id.' and delete_tag=0')->count();
   		$pageSize = ceil($count/$num);
   		
   		$result=$pOb->where('user_id='.$user_id.' and delete_tag=0')->order('id desc')->limit($tiao,$num)->select();
   		
   		$data=array();
   		$data['page']=$page;
   		$data['pageSize']=$pageSize;
   		$data['data']=$result;

   		if($result) {
   			$this->ajaxReturn($data);
   		}else{
   			$this->ajaxReturn($data);
   		}
   	}
   	
   	//我的购买记录
    public function buyList(){
        $this->display();
    }
    
    //购买记录分页
    public function buyListPage(){
    	$user_id=session('user_id')?session('user_id'):0;
    	$page=I('page');
    	$num=10;
    	$tiao=($page-1)*$num;
    	$count=$this->buy_model->where(array('user_id'=>$user_id,'delete_tag'=>0))->count();
    	$pageSize=ceil($count/$num);
        $result=$this->buy_model->where(array('user_id'=>$user_id, 'delete_tag'=>0))->limit($tiao,$num)->order('status asc,id desc')->select();
        $data=array();
        $data['page']=$page;
        $data['pageSize']=$pageSize;
        $data['data']=$result;
        if($result) {
        	$this->ajaxReturn($data);
        }else{
        	$this->ajaxReturn('nodata');
        }
    }
    
      //充值记录删除
      public function recoderDel(){
      	$id=$_GET['id'];
      	$act=$_GET['act'];
      	switch($act) {
      		case 'buy':
      			$obj=M('Buy');
      			break;
      		case 'pay':
      			$obj=M('Pay');
      			break;
      		case 'rebate':
	      		$obj=M('Rebate');
	      		break;	
	      	case 'withdraw':
	      		$obj=M('Withdraw');
	      		break;
      	}
      	$re=$obj->where('id='.$id)->save(array('delete_tag'=>1,'delete_time'=>time()));
      	if($re) {
      		$this->ajaxReturn('success');
      	}else{
      		$this->ajaxReturn('error');
      	}
      }
      
      //充值搜索页面
      public function paySearch(){
      	$this->display();
      }
      
      //充值查询 
      public function paySearchCheck(){
      	$search = I('search');  //那一页该显示那些数据
      	$pOb=M('Pay');
      	$result=$pOb->where('uid='.$search)->order('add_time desc')->select();
      	if($result) {
      		$this->ajaxReturn($result);
      	}else{
      		$this->ajaxReturn('false');
      	}
      	
      }
      
      //个人信息
      public function userInfo(){
      	$user_id=session('user_id')?session('user_id'):0;
      	$uOb=M('User');
      	$user=$uOb->where('user_id='.$user_id)->find();
       	$agent = M('Agent')->where("level=".$user['role'])->find();
      	$this->assign('user',$user);
      	$this->assign('agent',$agent);
      	$this->display(); 
      }
      
      //个人信息修改保存
      public function userInfoSave(){
      	 	$user_id=session('user_id')?session('user_id'):0;
	        $user = $this->user_model->where('user_id='.$user_id)->find();
	
	        $data = array();
	        $data['bank_name'] = I('bank_name');
	        $data['bank_card'] = I('bank_card');
	        $data['name'] = I('name');
	        $data['card_id'] = I('card_id');
	
	        $r=$this->user_model->where('user_id='.$user_id)->save($data);
	        if ($r) {
	            $this->ajaxReturn('success');
	        }else{
	            $this->ajaxReturn('error');
	        }
      }
      
      //修改密码的验证
      public function pwdCheck(){
      	$user_id=session('user_id')?session('user_id'):0;
      	$uOb=M('User');
      	$password=I('password');
      	$result=$uOb->where('user_id='.$user_id)->find();
      	
      	if($result['password'] == md5($password)) {
      		$this->ajaxReturn('success');
      	}else{
      		$this->ajaxReturn('error');
      	}
      	
      }
      //修改密码保存
      public function InSave(){
      	$user_id=session('user_id')?session('user_id'):0;
      	$password=I('new_repassword');
      	$uOb=M('User');
      	$result=$uOb->where('user_id='.$user_id)->save(array('password'=>md5($password)));
      	if($result) {
      		$this->ajaxReturn('success');
      	}else{
      		$this->ajaxReturn('error');
      	}
      	
      }
      
      //公告设置
      public function contentNotice(){
      	$url = C('API_URL').'/?act=getContent&name=notice';
      	$curl_res=get_curl($url);
      	$this->assign('content',$curl_res);
      	$this->display();
      }
      
      //公告的保存
      public function contentNoticeSave(){
      	$data=I('post.');
      	$url=C('API_URL').'/?act=setContent&content='.urlencode($data['content']).'&name=notice';
        $curl_res = get_curl($url); 
        if($curl_res['code']) {
        	$this->ajaxReturn('error');
        }else{
        	$this->ajaxReturn('success');
        }
      }
      
      //联系方式设置
      public function contentContact(){
      	$url = C('API_URL').'/?act=getContent&name=contact';
        $curl_res = get_curl($url);
        $this->assign('content',$curl_res);
        $this->display();
      }
      //联系方式的保存
      public function contentContactSave() {
      	$data = I('post.');
      	$url=C('API_URL').'/?act=setContent&content='.urlencode($data['content']).'&name=contact';
        $curl_res = get_curl($url);
        if($curl_res['code']) {
        	$this->ajaxReturn('error');
        }else{
        	$this->ajaxReturn('success');
        }
      }
      
      //消息设置
      public function contentMessage(){
      	$url = C('API_URL').'/?act=getContent&name=message';
        $curl_res = get_curl($url);
        $this->assign('content',$curl_res);
        $this->display();
      }
      
      //消息设置的保存
      public function contentMessageSave(){
      		$data=I('post.');
	        $url=C('API_URL').'/?act=setContent&content='.urlencode($data['content']).'&name=message';
	        $curl_res = get_curl($url);
	        if ($curl_res['code']) {
	            $this->ajaxReturn('error');
	        } else {
	            $this->ajaxReturn('success');
	        }
      }
      
      //活动邀请
      public function activityInvite() {
      	$url = C('API_URL').'/?act=getInviterResource';
        $curl_res = get_curl($url);
        $this->assign('inviter_resource', $curl_res[C('GAME_RESOURCE_KEY')]);
        $this->display();
      }
      
      //活动邀请保存
      public function activityInviteSave(){
      	$data = I('post.');
      	$name = C('GAME_RESOURCE_KEY');
        $url = C('API_URL').'/?act=setInviterResource&'.$name.'='.intval($data['inviter_resource']);
        $curl_res = get_curl($url);
        if ($curl_res['code']) {
            $this->ajaxReturn('error');
        } else {
            $this->ajaxReturn('success');
        }
      }
      
      //房间消耗
      public function activityCreateRoom() {
      	$url = C('API_URL').'/?act=getActivityCreateRoom';
        $curl_res = get_curl($url);
        $curl_res['start_time'] = date("Y-m-d H:i:s");
        $this->assign('setting', $curl_res);
        $this->display();
      }
      
      //房间消耗保存
      public function activityCreateRoomSave() {
        $data=I('post.');

        $query = array();
        $query['act'] = 'setActivityCreateRoom';
        $query['startTime'] = strtotime($data['start_time']);
        $query['endTime'] = strtotime($data['end_time']);
        $query['rate'] = $data['rate'];

        $url = C('API_URL').'/?'.http_build_query($query);
        $curl_res = get_curl($url);
        if ($curl_res['code']) {
            $this->ajaxReturn('error');
        } else {
            $this->ajaxReturn('success');
        }
    }
    
    //用户统计
    public function userStat(){
    	$url=C('API_URL').'/?act=queryUserStat';
    	$curl_res=get_curl($url);
    	$this->assign('stat',$curl_res);
    	$this->display();
    }
    //每日新用户注册
    public function realTimeDate() {
    	$this->display();
    }
   
	
	public function realTimePage() {
		$url=C('API_URL').'/?act=dayUserStat';
		$curl_res=get_curl($url);
		if($curl_res != null) {
			$this->ajaxReturn($curl_res['arr']);
		}else{
			$this->ajaxReturn('nodata');
		}
	}

	
    //最近玩家信息列表
    public function playerDetail(){
    	$hours = I('hours');
    	$url=C('API_URL').'/?act=queryNewUserList&hour='.$hours;
    	
    	$curl_res=get_curl($url);
//  	$arr = array_reverse($curl_res['arr']);
    	
    	if($hours) {
    		$this->ajaxReturn($curl_res['arr']);
    	}else{
    		$this->ajaxReturn('error');
    	}
    }
    //玩家信息
    public function playerInfo(){
        $this->display();
    }
    public function playersMore(){
        if(I('start')) {
            $action = I('action');
            $start = I('start');
            $page = I('page');
            $url=C('API_URL').'/?act=queryUserList&start='.$start.'&action='.$action."&page=".$page ;
            $curl_res=get_curl($url);
            if($curl_res) {
                $this->ajaxReturn($curl_res);
            }else{
                $this->ajaxReturn('error');
            }
        }else{
            $url=C('API_URL').'/?act=queryUserList';
            $curl_res=get_curl($url);
            if($curl_res['count']){
                $this->ajaxReturn($curl_res);
            }else{
                $this->ajaxReturn('error');
            }
        }
    }
    //玩家信息展示
    public function playerList(){
    	$this->display();
    }
    public function userStatPage() {
    	$hours=I('hours');
    	$url=C('API_URL').'/?act=queryNewUserList&hour='.$hours;
    	$curl_res=get_curl($url);
    	$num=10;
    	$result=count($curl_res['arr']);
    	$pnum=ceil($result/$num);
    	if($num) {
    		$this->ajaxReturn($pnum);
    	}else{
    		$this->ajaxReturn(0);
    	}
    }
    //代理比例设置
    public function agentUserRatio(){
    	$agent=M('Agent');
    	$data=array();
    	$result=$agent->where('level=2')->find();
    	$rebate=$agent->where('level=3')->find();
    	$data['slevel']=$result['agent_rebate1'];
    	$data['tlevel']=$rebate['agent_rebate1'];
    	$data['tlevell']=$rebate['agent_rebate2'];
    	
    	$this->assign($data);
    	$this->display();
    }
    //比例设置
    public function agentRatioSet(){
    	$srebate=I("srebate"); //二向一
    	$trebate=I("trebate"); //三向一
    	$trebatel=I("trebatel"); //三向二

    	$agent=M('Agent');

    	if($trebate == 0 && $trebatel == 0) {
            $rebate=$agent->where('level=3')->save(array('agent_rebate1'=> 0,'agent_rebate2'=>0));
        }else if($trebate || $trebatel) {
            $rebate=$agent->where('level=3')->save(array('agent_rebate1'=>$trebatel,'agent_rebate2'=>$trebate));
        }
        $result=$agent->where('level=2')->save(array('agent_rebate1'=>$srebate));

    	if($result || $rebate) {
    		$this->ajaxReturn('success');
    	}else{
    		$this->ajaxReturn('error');
    	}
    }

    //在线返利比例设置
    public function onLineRatio(){
        $agent = M('Agent');
        $data = array();
        $result1 = $agent->where('level=1')->find();
        $result2 = $agent->where('level=2')->find();
        $result3 = $agent->where('level=3')->find();
        $data['rebate1'] = $result1['online_rebate1'];
        $data['rebate2'] = $result2['online_rebate1'];
        $data['srebate2'] = $result2['online_rebate2'];
        $data['rebate3'] = $result3['online_rebate1'];
        $data['srebate3'] = $result3['online_rebate2'];
        $data['trebate3'] = $result3['online_rebate3'];

        $this->assign($data);
        $this->display();
    }

    //在线返利比例设置
    public function setOnlineRatio() {
        $data = I('post.');
        $agent = M('Agent');

        if($data['rebate3'] == 0 && $data['srebate3'] == 0 && $data['trebate3'] == 0) {
            $result3 = $agent->where('level=3')->save(array('online_rebate1'=>0,'online_rebate2'=>0,'online_rebate3'=>0));
        }else if($data['rebate3'] || $data['srebate3'] || $data['trebate3']) {
            $result3 = $agent->where('level=3')->save(array('online_rebate1'=>$data['rebate3'],'online_rebate2'=>$data['srebate3'],'online_rebate3'=>$data['trebate3']));
        }
        $result1 = $agent->where('level=1')->save(array('online_rebate1'=>$data['rebate1']));
        $result2 = $agent->where('level=2')->save(array('online_rebate1'=>$data['rebate2'],'online_rebate2'=>$data['srebate2']));

        if($result1 || $result2 || $result3) {
            $this->ajaxReturn('success');
        }else{
            $this->ajaxReturn('error');
        }
    }
    //销售记录
    public function agentUserSell() {
    	$this->display();
    }
    
    //销售记录分页
    public function agentUserSellPage(){
    	$page=I('page');
    	$pOb=M('Pay');
    	$num=10;
    	$tiao=($page-1)*$num;
    	$count=$pOb->where('delete_tag=0')->count();
    	$pageSize=ceil($count/$num);
    	$result=$pOb->where('delete_tag=0')->order('add_time desc')->limit($tiao,$num)->select();
  
    	$data=array();
    	$data['page']=$page;
    	$data['pageSize']=$pageSize;
    	$data['data']=$result;
    	if($result) {
    		$this->ajaxReturn($data);
    	}else{
    		$this->ajaxReturn('error');
    	}
    }
    
    //我的下级
    public function myNext(){
    	$this->display();
    }
    //下级详情
    public function myNextSearch(){
        $this->display();
    }
    //下家查询
    public function myNextSearchh() {
        $user_id=session('user_id')?session('user_id'):0;
        $uid = I('uid');
        $uOb = M('User');
        $aOb = M('Agent');
        $result = $uOb->where('refer='.$user_id.' and user_id='.$uid)->find();
        $data = array();
        if($result) {
            $agent = $aOb->where('level='.$result['role'])->field('agent_name')->find();
            $count = $uOb->where('refer='.$uid)->count();

            $url = C('API_URL').'/?act=queryUser&uid='.$uid;
            $curl_res = get_curl($url);
            if($curl_res['code'] != 12 && $curl_res != null) {
                $data['agent'] = $result;
                $data['msg'] = $curl_res;
                $data['count'] = $count;
                $data['agent_name'] = $agent['agent_name'];
                $this->ajaxReturn($data);
            }else{
                $this->ajaxReturn('error');
            }
        }else{
            $this->ajaxReturn('nodata');
        }
    }

    //下级支付详情
    public function agentNextPayDetail(){
        $user_id = I('user_id');
        $pOb = M('Pay');
        $result = $pOb->where('user_id='.$user_id)->order('add_time desc')->select();
        if($result) {
            $this->ajaxReturn($result);
        }else{
            $this->ajaxReturn('error');
        }
    }

    //下级购买详情
    public function agentNextBuyDetail(){
        $user_id = I('user_id');
        $bOb = M('Buy');
        $result = $bOb->where('user_id='.$user_id.' and delete_tag=0')->order('add_time desc')->select();
        if($result) {
            $this->ajaxReturn($result);
        }else{
            $this->ajaxReturn('error');
        }
    }
    //我的下级分页
    public function myNextPage(){
    	$user_id=session('user_id')?session('user_id'):0;
    	$uOb=M('User');
    	$result = $uOb->where('refer='.$user_id .' and status=1')->field('*,(total_rebate-total_withdraw) wait_withdraw')->select();
    	if($result) {
    		$this->ajaxReturn($result);
    	}else{
    		$this->ajaxReturn('nodata');
    	}
    }

    //下级审核
    public function myNextCheck() {
        $this->display();
    }

//    下级审核分页
    public function myNextCheckPage(){
        $user_id = session('user_id') ? session('user_id') : 0;
        $uOb = M('User');
        $result = $uOb->where('refer='.$user_id.' and status=0')->select();
        if($result) {
            $this->ajaxReturn($result);
        }else{
            $this->ajaxReturn('nodata');
        }
    }
    //下级审核通过
    public function myNextCheckPass(){
        $user_id = I('user_id');
        $uOb = M('User');
        $result = $uOb->where('user_id='.$user_id)->save(array('status'=>1));
        if($result) {
            $this->ajaxReturn('success');
        }else{
            $this->ajaxReturn('error');
        }

    }

    //代理审核不通过
    public function myNextCheckDel(){
        $user_id = I('user_id');
        $uOb = M('User');
        $result = $uOb->where('user_id='.$user_id)->delete();
        if($result) {
            $this->ajaxReturn('success');
        }else{
            $this->ajaxReturn('error');
        }
    }
    public function playerSearch(){
        $this->display();
    }
    public function playerSearchh(){
        $user_id=session('user_id')?session('user_id'):0;
        $uid = I('uid');
        $uOb=M('User');
        $result = $uOb->where('user_id='.$user_id)->find();
        $urole = $result['role'];

        $user=C('API_URL').'/?act=hasUser&uid='.$uid;
        $user_res=get_curl($user);

        if($user_res['data'] == false){
            $this->ajaxReturn('unexist');
        }else{
            $url=C('API_URL').'/?act=queryUser&uid='.$uid;
            $curl_res=get_curl($url);
            if($curl_res['gm'] && ($curl_res['gm']['level'] >= 1 && $curl_res['gm']['level'] <= $this->rules['agent_level'])) {
                $this->ajaxReturn('cannot');
            }else{
                $this->ajaxReturn($curl_res);
            }
        }
    }
    //下级
    public function myNextRole(){
        $user_id=session('user_id')?session('user_id'):0;
        $uid = I('uid');
        $uOb=M('User');
        $result = $uOb->where('user_id='.$user_id)->field('role')->find();
        if($result) {
            $data = array();
            if($result['role'] == 2) {
                $urll=C('API_URL').'/?act=setGM&uid='.$uid.'&role='.$this->rules['agent_level'];
                $curll_res=get_curl($urll);
            }else if($result['role'] == 1){
                $urll=C('API_URL').'/?act=setGM&uid='.$uid.'&role='.($this->rules['agent_level'] - 1);
                $curll_res=get_curl($urll);
            }

            if($curll_res['level'] == ($this->rules['agent_level'] - 1)) {
                $data['role'] = $this->rules['agent_level'] - 1;
            }else if($curll_res['level'] == $this->rules['agent_level']) {
                $data['role'] = $this->rules['agent_level'];
            }else{
                $this->ajaxReturn('failure');
            }
            $data['user_id'] = $uid;
            $password = mt_rand(100000, 999999);
            $data['password'] = md5($password);
            $data['create_time'] = time();
            $data['active_time'] = time();
            $data['status'] = 1;
            $data['state'] = 1;
            $data['refer'] = $user_id;
            $uOb->add($data);
            $record = array();
            $record['password'] = $password;
            $record['state'] = 'success';
            $this->ajaxReturn($record);
        }else{
            $this->ajaxReturn('error');
        }


//        if($curll_res['level'] == 2) {
//            $data = array();
//            $data['user_id'] = $uid;
//            $password = mt_rand(100000, 999999);
//            $data['password'] = md5($password);
//            $data['create_time'] = time();
//            $data['active_time'] = time();
//            $data['status'] = 1;
//            $data['state'] = 1;
//            $data['refer'] = $user_id;
//            $data['role'] = 2;
//            $uOb->add($data);
//            $reocrd = array();
//            $reocrd['password'] = $password;
//            $reocrd['state'] = 'success';
//            $this->ajaxReturn($reocrd);
//        }else{
//            $this->ajaxReturn('error');
//        }
    }

    //玩家专场
    public function playerPriv(){
        $uid = I('uid');
        $privilege = I('privilege');
        $url=C('API_URL').'/?act=setPrivilege&uid='.$uid.'&privilege='.$privilege;
        $curl_res=get_curl($url);
        if($curl_res && $curl_res['privilege'] == $privilege){
            $this->ajaxReturn('success');
        }else if($curl_res['code'] == 12) {
            $this->ajaxReturn('failure');
        }else{
            $this->ajaxReturn('error');
        }
    }

    //我的返利
    public function myRebateList(){
        $this->display();
    }
	//我的返利分页
	public function myRebateListPage(){
		$user_id=session('user_id')?session('user_id'):0;
		$page=I('page');
		$num=10;
		$tiao=($page-1)*$num;
		$count=M('Rebate')->alias('r')
    		->join('__BUY__ b on b.id=r.buy_id')
    		->join('__USER__ u on u.user_id=b.user_id')
    		->field('r.*,b.goods_id,b.amount,b.number')
    		->where('r.user_id='.$user_id.' and r.delete_tag=0')
    		->count();
    	$pageSize=ceil($count/$num);
    	$result=M('Rebate')->alias('r')
    		->join('__BUY__ b on b.id=r.buy_id')
    		->join('__USER__ u on u.user_id=b.user_id')
    		->field('r.*,b.goods_id,b.amount,b.number')
    		->where('r.user_id='.$user_id.' and r.delete_tag=0')
    		->limit($tiao,$num)
    		->order('r.add_time desc')
    		->select();
    	$data=array();
    	$data['page']=$page;
    	$data['pageSize']=$pageSize;
    	$data['data']=$result;
    	if($result) {
    		$this->ajaxReturn($data);
    	}else{
    		$this->ajaxReturn('nodata');
    	}
	}

	//在线返利
    public function onLineRebate() {
	    $this->display();
    }
    //在线返利记录
    public function getOnlineRebate() {
        $user_id=session('user_id')?session('user_id'):0;
        $rebate = M('Onlinerebate');
        $result = $rebate->where('user_id='.$user_id.' and delete_tag=0')->order('add_time desc')->select();
        if($result) {
            $this->ajaxReturn($result);
        }else{
            $this->ajaxReturn('error');
        }
    }
    //删除在线记录
    public function delOnline() {
	    $id = I('id');
        $user_id=session('user_id')?session('user_id'):0;
        $rebate = M('Onlinerebate');
        $result = $rebate->where('user_id='.$user_id.' and id='.$id)->delete();
        if($result) {
            $this->ajaxReturn('success');
        }else{
            $this->ajaxReturn('error');
        }
    }
	//我的业绩
	public function myStat() {
		$user_id=session('user_id')?session('user_id'):0;
		$uOb=M('User');
		$user=$uOb->field('total_withdraw,total_give,total_rebate,total_amount')->where('user_id='.$user_id)->find();
		
		$url=C('API_URL').'/?act=getResource&uid='.$user_id;
   		$curl_res=get_curl($url);
   		
   		$this->assign($user);
   		$this->assign('card',$curl_res['card']);
   		$this->display();
	}
	
	//提现申请   ???
	public function withdraw(){
		$user_id=session('user_id')?session('user_id'):0;
		$user=$this->user_model->field('total_rebate,role,total_withdraw,name,card_id,bank_card,bank_name,pay_num,wchat')->where('user_id='.$user_id)->find();
		$wait_withdraw=$user['total_rebate']-$user['total_withdraw'];
		if($wait_withdraw >= $this->rules['agent_withdraw_least']) {
			$this->assign('is_withdraw',"yes");
		}

		$this->assign('wait_withdraw',$wait_withdraw);
		$this->assign('rules',$this->rules);
		$this->assign('user_id',$user_id);
		$this->assign('user',$user);
		$this->display();
	}



//    public function withdraw(){
//	    $user_id=session('user_id')?session('user_id'):0;
//	    $result=M('Withdraw')->alias('w')
//            ->join('__USER__ u on u.user_id=w.user_id')
//            ->field('w.*,u.total_rebate,u.role,u.total_withdraw,u.name,u.card_id,u.bank_card,u.bank_name,u.pay_num,u.wchat')
//            ->where('u.user_id='.$user_id.' and w.status=1 and w.delete_tag=0')
//            ->find();
//	    $wait_withdraw = $result['total_rebate']-$result['total_withdraw'];
//	    if($wait_withdraw >= $this->rules['agent_withdraw_least']) {
//	        $this->assign('is_withdraw','yes');
//	        $this->assign('wait_withdraw',$wait_withdraw);
//	        $this->assign('rules',$this->rules);
//            $this->assign('user_id',$user_id);
//            $this->assign('user',$result);
//            $this->display();
//        }
//    }
	
	//提现申请的保存
	public function withdrawSave() {
		$user_id=session('user_id')?session('user_id'):0;
		$wOb=M('Withdraw');
		$uOb=M('User');
		$data=I('post.');
		if($data['payment'] == 1) {
			if($data['payment_no'] == '') {
				$this->ajaxReturn('npay_num');
			}else{
				$re=$uOb->where('user_id='.$user_id)->save(array('name'=>$data['name'],'pay_num'=>$data['payment_no']));
			} 
			
		}else if($data['payment'] == 2){
			if($data['payment_no'] == '') {
				$this->ajaxReturn('nwchat');
			}else{
				$re=$uOb->where('user_id='.$user_id)->save(array('name'=>$data['name'],'wchat'=>$data['payment_no']));
			}
		}else if($data['payment'] == 3) {
			if($data['payment_no'] == '') {
				$this->ajaxReturn('nbank_card');
			}else{				
				$re=$uOb->where('user_id='.$user_id)->save(array('name'=>$data['name'],'bank_name'=>$data['bank_name'],'bank_card'=>$data['payment_no']));
			}
		}

		$result=$wOb->add(array('user_id'=>$user_id,'withdraw'=>$data['withdraw'],'payment'=>$data['payment'],'payment_no'=>$data['payment_no'],'add_time'=>time()));

		if($result) {
			$uOb->where('user_id='.$user_id)->setInc('total_withdraw',$data['withdraw']);
			$this->ajaxReturn('success');
		}else{
			$this->ajaxReturn('error');
		}
	}
	
	//提现列表
	public function withdrawList() {
        $this->display();
	}
	//提现列表页面
	public function withdrawListPage(){
		$user_id=session('user_id')?session('user_id'):0;
        $wOb=M('Withdraw');
		$page=I('page');
		$num=10;
		$tiao=($page-1)*$num;
		$count=$wOb->where('user_id='.$user_id)->count();
		$pageSize=ceil($count/$num);
		$result=$wOb->where('user_id='.$user_id .' and delete_tag=0')->limit($tiao,$num)->order('status asc,id desc')->select();
		$data=array();
		$data['page']=$page;
		$data['pageSize']=$pageSize;
		$data['data']=$result;
		if($result) {
			$this->ajaxReturn($data);
		}else{
			$this->ajaxReturn('nodata');
		}
	}
	
	
	//提现审核页面
	public function withdrawReview() {
        $this->display();
	}
	
	//提现分页
	public function withdrawReviewPage(){
		$page=I('page');
		$num=10;
		$wOb=M('Withdraw');
		$tiao=($page-1)*$num;
		$count=$wOb->alias('w')
            ->join('__USER__ u on u.user_id=w.user_id')
            ->field('u.tel,w.*')
            ->where('w.status=0')
            ->count();
        $pageSize=ceil($count/$num);
        $result=$wOb->alias('w')
            ->join('__USER__ u on u.user_id=w.user_id')
            ->field('u.tel,w.*')
            ->where('w.status=0')
            ->order('w.add_time desc')
            ->limit($tiao,$num)
            ->select();
        $data=array();
        $data['page']=$page;
        $data['pageSize']=$pageSize;
        $data['data']=$result;
        if($result) {
        	$this->ajaxReturn($data);
        }else{
        	$this->ajaxReturn('error');
        }
	}
	//获取提现的详情
    public function getWithdraw() {
	    $ids = I('ids');
	    $this->assign('ids',$ids);
	    $this->display();
    }

	//删除提现申请
	public function withdrawReviewDel(){
		$id=$_GET['id'];
		$wOb=M('Withdraw');
		$uOb=M('User');
		$result=$wOb->where('id='.$id)->save(array('delete_tag'=>1,'status'=>1,'delete_time'=>time(),'modify_time'=>time()));
		$data = $wOb->where('id='.$id)->find();
        $record=$uOb->where('user_id='.$data['user_id'])->setDec('total_withdraw',$data['withdraw']);
        if($result && $record) {
			$this->ajaxReturn('success');
		}else{
			$this->ajaxReturn('error');
		}
	}
	
	//提现信息展示
	public function withdrawReviewShow() {
		$id=$_GET['id'];
		$result=M('Withdraw')->alias('w')
				->join('__USER__ u on u.user_id=w.user_id')
				->field('w.*,u.name,u.pay_num,u.bank_name,u.bank_card,u.wchat')
				->where('w.id='.$id)
				->find();
		if($result) {
			$this->ajaxReturn($result);
		}else{
			$this->ajaxReturn('error');
		}
	}
	
	//提现审核
	public function withdrawReviewSave() {
		$id=$_GET['id'];
        $wOb=M('Withdraw');
        $re=$wOb->where('id='.$id)->save(array('status'=>1,'modify_time'=>time()));
        if ($re) {
            $this->ajaxReturn('success');
        }else{
            $this->ajaxReturn('error');
        }
	}
	
	//提现记录
	 public function uwithdrawList(){
        $wOb=M('Withdraw');
        $re=$wOb->alias('w')
            ->join('__USER__ u on u.user_id=w.user_id')
            ->field('u.tel,w.*')
            ->where('w.status=1')
            ->order('w.modify_time desc')
            ->select();
        $data=showPage($re);
        $arr=$data['arr'];
        $str=$data['pageStr'];
        $this->assign('arr',$arr);
        $this->assign('str',$str);
        $this->display();
    }
    
    //提现记录分页
    public function uwithdrawListPage(){
    	$page=I('page');
    	$num=10;
    	$tiao=($page-1)*$num;
    	$wOb=M('Withdraw');
    	$count=$wOb->alias('w')
            ->join('__USER__ u on u.user_id=w.user_id')
            ->field('u.tel,w.*')
            ->where('w.status=1')
            ->count();
        $pageSize=ceil($count/$num);
        $result=$wOb->alias('w')
            ->join('__USER__ u on u.user_id=w.user_id')
            ->field('u.tel,w.*')
            ->where('w.status=1')
            ->limit($tiao,$num)
            ->order('w.modify_time desc')
            ->select();   
        $data=array();
        $data['page']=$page;
        $data['pageSize']=$pageSize;
        $data['data']=$result;
        if($result) {
        	$this->ajaxReturn($data);
        } else {
        	$this->ajaxReturn('error');
        }
    }
    //代理月购买量
    public function monthDate() {
    	$this->display();
    }
    public function monthDateSave() {
        $bOb=M('Buy');
        $pOb=M('Pay');
    	$pay_number=$pOb->where('status=1 and delete_tag=0')->field('SUM(number) as total_pay,FROM_UNIXTIME(add_time,"%Y-%m") as month')->group('month')->order('month desc')->select();
        $buy_number=$bOb->where('status=1 and delete_tag=0')->field('SUM(amount) as total_buy,FROM_UNIXTIME(add_time,"%Y-%m") as month')->group('month')->order('month desc')->select();
        $data = array_merge_recursive($buy_number,$pay_number);
        $res = array();
        foreach ($data as $k => $v){
            if($v['total_buy']) {
                $res[$v['month']]['total_buy'] = $v['total_buy'];
            }
            if($v['total_pay']) {
                $res[$v['month']]['total_pay'] = $v['total_pay'];
            }
        }
        if($res) {
            $this->ajaxReturn($res);
        }else{
            $this->ajaxReturn('error');
        }
    }
    //游戏ID月购买量
    public function monthStat() {
        $this->display();
    }
    public function monthStatData(){
        $user_id=session('user_id')?session('user_id'):0;
        $bOb=M('Buy');
        $pOb=M('Pay');
        $buy_number=$bOb->where('user_id='.$user_id.' and status=1')->field('SUM(amount) as total_buy,FROM_UNIXTIME(add_time,"%Y-%m") as month')->group('month')->order('month desc')->select();
        $pay_number=$pOb->where('user_id='.$user_id.' and status=1 and delete_tag=0')->field('SUM(number) as total_pay,FROM_UNIXTIME(add_time,"%Y-%m") as month')->group('month')->order('month desc')->select();
        $data = array_merge_recursive($buy_number,$pay_number);
        $res = array();
        foreach ($data as $k => $v){
            if($v['total_buy']) {
                $res[$v['month']]['total_buy'] = $v['total_buy'];
            }
            if($v['total_pay']) {
                $res[$v['month']]['total_pay'] = $v['total_pay'];
            }
        }
        if($res) {
            $this->ajaxReturn($res);
        }else{
            $this->ajaxReturn('error');
        }
    }
    public function upload() {
        $upload = new \Think\Upload();
        $upload->maxSize = 3145728;
        $upload->exts = array('jpg','gif','png','jpeg');
        $upload->rootPath = './Upload/';
        $upload->savePath = '';
        $info = $upload->upload();
        if(!$info) {
            $this->error($upload->getError());
        }else{
            $img_path = './Upload/'.$info['goods_img']['savepath'];
//           echo ($img_path);
            $this->success('上传成功');
        }
    }
    //群组成员
    public function groupMember(){
        $this->display();
    }

    //获取成员列表
    public function getGroupMember(){
        $user_id = session('user_id')?session('user_id'):0;
        if(I('start')) {
            $action = I('action');
            $start = I('start');
            $page = I('page');
            $url = C('API_URL').'/?act=queryMemberOfGroup&start='.$start.'&action='.$action.'&page='.$page.'&gid='.$user_id;
            $curl_res = get_curl($url);
            if($curl_res['arr']) {
                $this->ajaxReturn($curl_res);
            }else{
                $this->ajaxReturn('error');
            }
        }else{
            $url = C('API_URL').'/?act=queryMemberOfGroup&gid='.$user_id;
            $curl_res = get_curl($url);
            if($curl_res['arr']) {
                $this->ajaxReturn($curl_res);
            }else{
                $this->ajaxReturn('error');
            }
        }
    }

//    删除群组成员
    public function delCroupMember(){
        $user_id = session('user_id')?session('user_id'):0;
        $uid = I('uid');
        $url = C('API_URL').'/?act=delMemberOfGroup&gid='.$user_id.'&uid='.$uid;
        $curl_res = get_curl($url);
        if($curl_res['code']['result'] == true) {
            $this->ajaxReturn('success');
        }else{
            $this->ajaxReturn('error');
        }
    }

//    俱乐部
    public function clubMember(){
        $this->display();
    }
    public function clubMemberList(){
        $user_id = session('user_id')?session('user_id'):0;
        if(I('start')) {
            $action = I('action');
            $start = I('start');
            $page = I('page');
            $url=C('API_URL').'/?act=queryClubMemberList&start='.$start.'&action='.$action.'&page='.$page.'&uid='.$user_id;
            $curl_res=get_curl($url);
            if($curl_res['arr']) {
                $this->ajaxReturn($curl_res);
            }else{
                $this->ajaxReturn('error');
            }
        }else{
            $url=C('API_URL').'/?act=queryClubMemberList&uid='.$user_id;
            $curl_res=get_curl($url);
            if($curl_res['arr']) {
                $this->ajaxReturn($curl_res);
            }else{
                $this->ajaxReturn('error');
            }
        }
    }

//    意见反馈
    public function feedBack(){
        $this->display();
    }
//   意见反馈的数据处理
    public function feedBackData(){
        $fOb = M('Feedback');
        $result = $fOb->where('status = 0')->field('*')->order('add_time desc')->select();
        if($result) {
            $this->ajaxReturn($result);
        }else{
            $this->ajaxReturn('nodata');
        }
    }
//    操作某一条数据
    public function feedBackList() {
        $id = I('id');
        $fOb = M('Feedback');
        $result = $fOb->where('id='.$id)->find();
        if($result) {
            $this->ajaxReturn($result);
        }else{
            $this->ajaxReturn('nodata');
        }
    }
    public function feedBackPass(){
        $id = I('id');
        $fOb = M('Feedback');
        $result = $fOb->where('id='.$id)->save(array('status'=>1));
        if($result) {
            $this->ajaxReturn('success');
        }else{
            $this->ajaxReturn('error');
        }
    }
//    反馈处理的列表页面
    public function feedBackLists() {
        $this->display();
    }
    //返回处理列表数据
    public function feedBackListDate(){
        $fOb = M('Feedback');
        $result = $fOb->where('status = 1')->field('*')->order('add_time desc')->select();
        if($result) {
            $this->ajaxReturn($result);
        }else{
            $this->ajaxReturn('nodata');
        }
    }

//    删除反馈
    public function feedBackDel() {
        $id = I('id');
        $fOb = M('Feedback');
        $result = $fOb->where('id='.$id)->delete();
        if($result) {
            $this->ajaxReturn('success');
        }else{
            $this->ajaxReturn('error');
        }
    }

    //比赛记录
    public function gameRecord() {
        $this->display();
    }
    //获取比赛的信息
    public function getGameRecord() {
        $record = I('record');
        $url=C('API_URL').'/?act=getMatchInfoDetail&matchSN='.$record;
        $curl_res=get_curl($url);
        if($curl_res && $curl_res['code'] != 20) {
            $this->ajaxReturn($curl_res);
        }else{
            $this->ajaxReturn('error');
        }
    }

    public function gameRecordList() {
        $this->display();
    }

//    发放设置
    public function setGameRecordMark() {
        $matchSN = I('matchSN');
        $mark = I('mark');
        $url=C('API_URL').'/?act=updateMatchPlayerMark&matchSN='.$matchSN.'&mark='.$mark;
        $curl_res=get_curl($url);
        if($curl_res['code'] == 0) {
            $this->ajaxReturn('success');
        }else{
            $this->ajaxReturn('error');
        }
    }

//    记录列表
    public function getGameRecordList() {
        if(I('start')) {
            $start = I('start');
            $action = I('action');
            $page = I('page');
            $url=C('API_URL').'/?act=getMatchPlayerList&start='.$start.'&action='.$action."&page=".$page ;
            $curl_res=get_curl($url);
            if($curl_res && $curl_res['code'] != 20) {
                $this->ajaxReturn($curl_res);
            }else{
                $this->ajaxReturn('error');
            }
        }else{
            $url=C('API_URL').'/?act=getMatchPlayerList';
            $curl_res=get_curl($url);
            if($curl_res && $curl_res['code'] != 20) {
                $this->ajaxReturn($curl_res);
            }else{
                $this->ajaxReturn('error');
            }
        }
    }
    //广告弹窗
    public function advSet(){
        $this->display();
    }

    //弹窗默认信息获取
    public function advGet() {
        $url=C('API_URL').'/?act=getGlobalConfig&name=advertisement';
        $curl_res=get_curl($url);
        if($curl_res != '' || $curl_res != null) {
            $this->ajaxReturn($curl_res);
        }else{
            $this->ajaxReturn('error');
        }
    }
    //弹窗设置
    public function advSets(){
        $show = I('adv');
        $url=C('API_URL').'/?act=setAdvertisement&show='.$show;
        $curl_res=get_curl($url);
        if($curl_res['show'] == $show) {
            $this->ajaxReturn('success');
        }else{
            $this->ajaxReturn('error');
        }

    }
    //代理充值排行页面
    public function agentPayBank(){
        $this->display();
    }
    //代理充值的记录
    public function agentPayBankData(){
        $pOb = M('Pay');
        $user_id = session('user_id')?session('user_id'):0;
        $now_time = time();
        $time = $now_time - 604800;
        $map['add_time'] = array(array('EGT',$time),array('ELT',$now_time));
        $map['delete_tag'] = 0;
        $map['user_id'] = $user_id;

        $result=$pOb->where($map)
                    ->group('uid')
                    ->field('*,SUM(number) as total_number')
                    ->order('total_number desc')->select();
        if($result) {
            $this->ajaxReturn($result);
        }else{
            $this->ajaxReturn('nodata');
        }
    }

        //代理根据时间查询
        public function agentPayBankSearch() {
            $user_id = session('user_id')?session('user_id'):0;
            $start_time = I('start_time');
            $end_time = I('end_time');
            $pOb = M('Pay');
            $map['add_time'] = array(array('EGT',$start_time),array('ELT',$end_time));
            $map['delete_tag'] = 0;
            $map['user_id'] = $user_id;

            $result = $pOb->where($map)->group('uid')->field('*,SUM(number) as total_number')->order('total_number desc')->select();

            if($result) {
                $this->ajaxReturn($result);
            }else{
                $this->ajaxReturn('nodata');
            }
        }


    //总后台充值排行
    public function managePayBank(){
        $this->display();
    }
    public function managePayBankData(){
        $pOb = M('Pay');
        $now_time = time();
        $time = $now_time - 604800;
        $map['add_time'] = array(array('EGT',$time),array('ELT',$now_time));
        $map['delete_tag'] = 0;

        $result=$pOb->where($map)
            ->group('uid')
            ->field('*,SUM(number) as total_number')
            ->order('total_number desc')->select();
        if($result) {
            $this->ajaxReturn($result);
        }else{
            $this->ajaxReturn('nodata');
        }
    }

    //总代理根据时间查询排行
    public function managePayBankSearch(){
        $pOb = M('Pay');
        $start_time = I('start_time');
        $end_time = I('end_time');
        $map['add_time'] = array(array('EGT',$start_time),array('ELT',$end_time));
        $map['delete_tag'] = 0;

        $result = $pOb->where($map)
            ->group('uid')
            ->field('*,SUM(number) as total_number')
            ->order('total_number desc')
            ->select();
        if($result) {
            $this->ajaxReturn($result);
        }else{
            $this->ajaxReturn('nodata');
        }
    }

    //日志消耗
    public function logExpend() {
        $this->display();
    }

    //详细的日志消耗
    public function logExpendDetail(){
        $uid = I('uid');
        $start_time = I('start_time');
        $end_time = I('end_time');
        $url=C('API_URL').'/?act=queryUserCardLog&uid='.$uid.'&startTime='.$start_time.'&endTime='.$end_time;
        $curl_res=get_curl($url);
        if($curl_res) {
            $this->ajaxReturn($curl_res);
        }else{
            $this->ajaxReturn('error');
        }
    }

    //消耗统计
    public function expendTotal(){
        $this->display();
    }

    //消耗统计
    public function getsExpendTotal(){
        $start_time = I('start_time');
        $end_time = I('end_time');
        $url=C('API_URL').'/?act=queryResourceLogSum&start='.$start_time.'&end='.$end_time;
        $curl_res=get_curl($url);
        if($curl_res && $curl_res['code'] != 20) {
            $this->ajaxReturn($curl_res);
        }else{
            $this->ajaxReturn('error');
        }
    }
    //我的下级日志消耗
    public function myNextLog(){
        $this->display();
    }

//    消耗详情
    public function everyExpend(){
        $start_time = I('start_time');
        $end_time = I('end_time');
        $url=C('API_URL').'/?act=queryResourceLogSumDetail&start='.$start_time.'&end='.$end_time;
        $curl_res=get_curl($url);
        if($curl_res && $curl_res['code'] != 20) {
            $this->ajaxReturn($curl_res);
        }else{
            $this->ajaxReturn('error');
        }
    }

//    下级消耗统计
    public function getmyNextLog(){
        $user_id = session('user_id')?session('user_id'):0;
        $start_time = I('start_time');
        $end_time = I('end_time');
        $url=C('API_URL').'/?act=queryClubMemberCostList&uid='.$user_id.'&start='.$start_time.'&end='.$end_time;
        $curl_res=get_curl($url);
        if($curl_res && $curl_res['code'] != 20) {
            $this->ajaxReturn($curl_res);
        }else{
            $this->ajaxReturn('error');
        }
    }
    public function myNextChecked() {
        $user_id = session('user_id')?session('user_id'):0;
        $uOb = M('User');
        $uid = I('uid');
        $url=C('API_URL').'/?act=hasUser&uid='.$uid;
        $curl_res=get_curl($url);
        if($curl_res['data'] == true) {
            $urll=C('API_URL').'/?act=queryUser&uid='.$uid;
            $curll_res=get_curl($urll);
            if($curll_res) {
                if(array_key_exists('gm', $curll_res) && ($curll_res[gm]['level'] >= 1 && $curll_res['gm']['level'] <= $this->rules['agent_level'])) {
                    $result = $uOb->where('refer='.$user_id.' and user_id='.$uid)->find();
                    if($result) {
                        $this->ajaxReturn('success');
                    }else{
                        $this->ajaxReturn('notagent');
                    }
                }else if($curll_res['gm']['level'] == 99) {
                    $this->ajaxReturn('admin');
                }
            }else{
                $this->ajaxReturn('error');
            }
        }else{
            $this->ajaxReturn('unexist');
        }
    }

    public function myNextLogDetail(){
        $uid = I('uid');
        $start_time = I('start_time');
        $end_time = I('end_time');
        $url=C('API_URL').'/?act=queryUserCardLog&uid='.$uid.'&startTime='.$start_time.'&endTime='.$end_time;
        $curl_res=get_curl($url);
        if($curl_res) {
            $this->ajaxReturn($curl_res);
        }else{
            $this->ajaxReturn('nodata');
        }
    }
    // 在线支付详情
    public function onLinePayDetail(){
        $this->display();
    }

    //    在线支付查询
    public function onLinePayList() {
        if(I('uid') && I('start')) {
            $action = I('action');
            $start = I('start');
            $page= I('page');
            $uid = I('uid');
            $url = C('API_URL').'/?act=queryUserPayList&start='.$start.'&action='.$action.'&page='.$page.'&uid='.$uid;
            $curl_res = get_curl($url);
            if($curl_res && $curl_res['code'] != 20) {
                $this->ajaxReturn($curl_res);
            }else{
                $this->ajaxReturn('error');
            }
        }else if(I('uid') && !I('start')){
            $uid=I('uid');
            $url=C('API_URL').'/?act=queryUserPayList&uid='.$uid;
            $curl_res=get_curl($url);
            if($curl_res && $curl_res['code'] != 20) {
                $this->ajaxReturn($curl_res);
            }else{
                $this->ajaxReturn('error');
            }
        }

        if(I('start')) {
            $action = I('action');
            $start = I('start');
            $page= I('page');
            $url = C('API_URL').'/?act=queryUserPayList&start='.$start.'&action='.$action.'&page='.$page;
            $curl_res = get_curl($url);
//            var_dump($curl_res);
            if($curl_res && $curl_res['code'] != 20) {
                $this->ajaxReturn($curl_res);
            }else{
                $this->ajaxReturn('error');
            }
       }else{
           $url=C('API_URL').'/?act=queryUserPayList';
           $curl_res=get_curl($url);
           if($curl_res && $curl_res['code'] != 20) {
               $this->ajaxReturn($curl_res);
           }else{
               $this->ajaxReturn('error');
           }
       }
    }
    //代理下级玩家的在线充值记录
    public function getAgentPay(){
        $user_id = session('user_id')?session('user_id'):0;
        $url=C('API_URL').'/?act=queryInvitedUsers&uid='.$user_id;
        $curl_res=get_curl($url);
        if($curl_res){
           $this->ajaxReturn($curl_res);
        }else{
            $this->ajaxReturn('error');
        }
    }

//  时间
    public function getAgentOnlinePay(){
        $user_id = session('user_id')?session('user_id'):0;
        $start_time = I('start_time');
        $end_time = I('end_time');
        if($start_time && $end_time) {
            $url=C('API_URL').'/?act=queryUsersPaySum&uid='.$user_id.'&start='.$start_time.'&end='.$end_time;
            $curl_res=get_curl($url);
        }else{
            $url=C('API_URL').'/?act=queryUsersPaySum&uid='.$user_id;
            $curl_res=get_curl($url);
        }

        if($curl_res['code'] != 20 && $curl_res != null) {
            $this->ajaxReturn($curl_res);
        }else{
            $this->ajaxReturn('error');
        }
    }

//    消息推送
    public function msgPush(){
        $this->display();
    }
//    消息获取
    public function getMsgPush(){
        $url = C('API_URL').'/?act=getLastPush';
        $curl_res = get_curl($url);
        if($curl_res && $curl_res['code'] != 20) {
            $this->ajaxReturn($curl_res);
        }else{
            $this->ajaxReturn('error');
        }
    }

//消息推送设置
    public function setMsgPush(){
        $title = I('title');
        $cbody = I('cbody');
        $url = C('API_URL').'/?act=pushToAll&title='.$title.'&body='.$cbody.'&deviceType=ANDROID';
        $curl_res = get_curl($url);
        if($curl_res['code'] == 0) {
            $this->ajaxReturn('success');
        }else{
            $this->ajaxReturn('error');
        }
    }

//    数据导出
    public function test1(){
        $this->display();
    }
//    导出玩家数据
    public function expPlayer(){
        $xlsName = '所有玩家在线充值数据';
        $xlsCell = array(
            array('uid','玩家ID'),
            array('money','支付金额'),
            array('status','支付状态'),
            array('item','商品编号'),
            array('time','支付时间')
        );
        $url=C('API_URL').'/?act=queryAllUserPayment';
        $curl_res=get_curl($url);
        
        $xlsData = array();
        for ($i=0;$i<count($curl_res);++$i){
            array_push($xlsData,$curl_res[$i]);
        }
        foreach ($xlsData as $k => $v) {
            $xlsData[$k]['status'] = $v['status'] == 1?'成功':'失败';
            $xlsData[$k]['time'] = date('Y-m-d H:i:s',$v['time']/1000);
        }
        $this->exportExcel($xlsName,$xlsCell,$xlsData);
    }

//    导出某一个玩家的数据
    public function expEvePlayer(){
        $uid = I('uid');
        $xlsName = '玩家'.$uid.'在线充值数据';
        $xlsCell = array(
            array('uid','玩家ID'),
            array('money','支付金额'),
            array('status','支付状态'),
            array('item','商品编号'),
            array('time','支付时间')
        );
        $url=C('API_URL').'/?act=queryAllUserPayment&uid='.$uid;
        $curl_res=get_curl($url);

        $xlsData = array();
        for($i=0;$i<count($curl_res);++$i) {
            array_push($xlsData,$curl_res[$i]);
        }
        foreach ($xlsData as $k => $v) {
            $xlsData[$k]['status'] = $v['status'] = 1?'成功':'失败';
            $xlsData[$k]['time'] = date('Y-m-d H:i:s',$v['time']/1000);
        }
        $this->exportExcel($xlsName,$xlsCell,$xlsData);
    }

    //实时数据导出
    public function relTimeExport(){
        $xlsName = '新注册用户实时数据';
        $xlsCell = array(
            array('count','新用户注册数'),
            array('num1','次日留存数'),
            array('num3','三日留存数'),
            array('num7','七日留存数'),
            array('day','注册日期')
        );
        $url=C('API_URL').'/?act=dayUserStat';
        $curl_res=get_curl($url);

        $xlsData = array();

        for ($i=0;$i<count($curl_res['arr']);++$i) {
            array_push($xlsData,$curl_res['arr'][$i]);
        }
        $this->exportExcel($xlsName,$xlsCell,$xlsData);
    }

    //充值数据的导出
    public function payExport(){
        $user_id = I('user_id');
        $xlsName = '游戏ID为'.$user_id.'的充值数据';
        $xlsCell = array(
            array('id','充值编号'),
            array('user_id','充值者ID'),
            array('uid','被充值者ID'),
            array('number','充值金额'),
            array('status','充值状态'),
            array('add_time','充值时间')
        );
        $xlsModel = M('Pay');
        $xlsData = $xlsModel->where('uid='.$user_id)->order('add_time desc')->select();
        foreach ($xlsData as $k => $v) {
            $xlsData[$k]['status'] = $v['status'] = 1?'成功':'失败';
            $xlsData[$k]['add_time'] = date('Y-m-d H:i',$v['add_time']);
        }
        $this->exportExcel($xlsName,$xlsCell,$xlsData);
    }

    //日志消耗导出
    public function logExpExport(){
        $uid = I('uid');
        $xlsName = '游戏UID为'.$uid.'的日志消耗详情';
        $xlsCell = array(
            array('uid','游戏UID'),
            array('card','更改数量'),
            array('s.card','剩余数量'),
            array('r','更改原因'),
            array('t','更改时间')
        );
        $url=C('API_URL').'/?act=queryUserCardLog&uid='.$uid;
        $curl_res=get_curl($url);

        $xlsData = array();
        for($i = 0;$i < count($curl_res['arr']);++$i) {
            array_push($xlsData,$curl_res['arr'][$i]);
        }

        foreach($xlsData as $k => $v) {
            switch ($v['r']) {
                case 'pay':
                    $xlsData[$k]['r'] = '购买';
                    break;
                case 'payTo':
                    $xlsData[$k]['r'] = '给人充值';
                case 'gmAdd':
                    $xlsData[$k]['r'] = '后台添加';
                    break;
                case 'gmAlter':
                    $xlsData[$k]['r'] = '后台修改';
                    break;
                case 'exchangeCoin':
                    $xlsData[$k]['r'] = '兑换金币';
                    break;
                case 'roomFinished':
                    $xlsData[$k]['r'] = '房间消耗';
                    break;
                case 'SevenDayReward':
                    $xlsData[$k]['r'] = '签到奖励';
                    break;
                case 'initResource':
                    $xlsData[$k]['r'] = '初始资源';
                    break;
                case 'XYBBKGameCost':
                    $xlsData[$k]['r'] = '分享奖励';
                    break;
                case 'shareReward':
                    $xlsData[$k]['r'] = '幸运比比看加注金砖使用';
                    break;
                default:
                    $xlsData[$k]['r'] = $v['r'];
            }
        }
        $this->exportExcel($xlsName,$xlsCell,$xlsData);
    }

    //代理列表导出
    public function userListExport(){
        $xlsName = '所有代理的列表';
        $xlsCell = array(
            array('user_id','代理ID'),
            array('tel','手机号码'),
            array('name','真实姓名'),
            array('refer','推荐者'),
            array('role','代理级别'),
            array('total_amount','总消费额'),
            array('total_rebate','总返利额'),
            array('total_give','总充值量'),
            array('wait_withdraw','待提现额'),
            array('create_time','注册时间')
        );
        $xlsModel = M('User');
        $xlsData = $xlsModel->where('status=1 and role<10')->field('*,(total_rebate - total_withdraw) wait_withdraw')->order('create_time desc')->select();
        foreach ($xlsData as $k => $v) {
            $xlsData[$k]['refer'] = $v['refer'] == 0?'无':$v['refer'];
            switch ($v['role']) {
                case '1':
                $xlsData[$k]['role'] = '一级代理';
                break;
                case '2':
                    $xlsData[$k]['role'] = '二级代理';
                    break;
                case '3':
                    $xlsData[$k]['role'] = '三级代理';
                    break;
                default:
                    $xlsData[$k]['role'] = '管理员';
            }
            $xlsData[$k]['create_time'] = date('Y-m-d H:i',$v['create_time']);
        }
        $this->exportExcel($xlsName,$xlsCell,$xlsData);
    }

    //购买数据的导出
    public function buyListExport(){
        $start_time = I('start_time');
        $end_time = I('end_time');
        $xlsName = '购买详情的数据';
        $xlsCell = array(
          array('user_id','代理ID'),
          array('number','购买数量'),
          array('status','购买状态'),
          array('add_time','购买时间')
        );
        $xlsModel = M('Buy');
        $xlsData = $xlsModel->where('delete_tag=0 and add_time>='.$start_time.' and add_time<='.$end_time)->order('add_time desc')->select();
        foreach ($xlsData as $k => $v) {
            $xlsData[$k]['status'] = $v['status'] = 1?'成功':'失败';
            $xlsData[$k]['add_time'] = date('Y-m-d H:i',$v['add_time']);
        }
        $this->exportExcel($xlsName,$xlsCell,$xlsData);
    }


//    玩家列表信息导出
    public function userInfoExport(){
        $start_time = I('start_time');
        $end_time = I('end_time');
        $xlsName = '玩家的数据';
        $xlsCell = array(
            array('_id','玩家ID'),
            array('name','微信昵称'),
            array('card','房卡张数'),
            array('coin','金币数量'),
            array('createTime','创建时间')
        );
        $url=C('API_URL').'/?act=queryUserListByCreateTime&startTime='.$start_time.'&endTime='.$end_time;
        $curl_res=get_curl($url);
        $xlsData = array();
        for($i = 0;$i < count($curl_res['arr']);++$i) {
            array_push($xlsData,$curl_res['arr'][$i]);
            $xlsData[$i]['name'] = $xlsData[$i]['info']['name'];
            $xlsData[$i]['createTime'] = $xlsData[$i]['info']['createTime'];
            $xlsData[$i]['card'] = $xlsData[$i]['status']['card'];
            $xlsData[$i]['coin'] = $xlsData[$i]['status']['coin'];
        }
        foreach ($xlsData as $k => $v) {
            $xlsData[$k]['createTime'] = date('Y-m-d H:i',$v['createTime']);
        }
        $this->exportExcel($xlsName,$xlsCell,$xlsData);
    }

    public function expendExport(){
        $start_time = I('start_time');
        $end_time = I('end_time');
        $xlsName = '日志消耗的数据';
        $xlsCell = array(
            array('uid','游戏UID'),
            array('card','总消耗数')
        );
        $url=C('API_URL').'/?act=queryResourceLogSumDetail&start='.$start_time.'&end='.$end_time;
        $xlsData=get_curl($url);
        $this->exportExcel($xlsName,$xlsCell,$xlsData);
    }
//    function impUser(){
//        if (!empty($_FILES)) {
//            import("@.ORG.UploadFile");
//            $config=array(
//                'allowExts'=>array('xlsx','xls'),
//                'savePath'=>'./Public/upload/',
//                'saveRule'=>'time',
//            );
//            $upload = new UploadFile($config);
//            if (!$upload->upload()) {
//                $this->error($upload->getErrorMsg());
//            } else {
//                $info = $upload->getUploadFileInfo();
//
//            }
//
//            vendor("PHPExcel.PHPExcel");
//            $file_name=$info[0]['savepath'].$info[0]['savename'];
//            $objReader = PHPExcel_IOFactory::createReader('Excel5');
//            $objPHPExcel = $objReader->load($file_name,$encode='utf-8');
//            $sheet = $objPHPExcel->getSheet(0);
//            $highestRow = $sheet->getHighestRow(); // 取得总行数
//            $highestColumn = $sheet->getHighestColumn(); // 取得总列数
//            for($i=3;$i<=$highestRow;$i++)
//            {
//                $data['account']= $data['truename'] = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
//                $sex = $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
//                // $data['res_id']    = $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
//                $data['class'] = $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
//                $data['year'] = $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
//                $data['city']= $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
//                $data['company']= $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
//                $data['zhicheng']= $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
//                $data['zhiwu']= $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
//                $data['jibie']= $objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue();
//                $data['honor']= $objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue();
//                $data['tel']= $objPHPExcel->getActiveSheet()->getCell("M".$i)->getValue();
//                $data['qq']= $objPHPExcel->getActiveSheet()->getCell("N".$i)->getValue();
//                $data['email']= $objPHPExcel->getActiveSheet()->getCell("O".$i)->getValue();
//                $data['remark']= $objPHPExcel->getActiveSheet()->getCell("P".$i)->getValue();
//                $data['sex']=$sex=='男'?1:0;
//                $data['res_id'] =1;
//
//                $data['last_login_time']=0;
//                $data['create_time']=$data['last_login_ip']=$_SERVER['REMOTE_ADDR'];
//                $data['login_count']=0;
//                $data['join']=0;
//                $data['avatar']='';
//                $data['password']=md5('123456');
//                M('Member')->add($data);
//
//            }
//            $this->success('导入成功！');
//        }else
//        {
//            $this->error("请选择上传的文件");
//        }
//    }
}


